local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent());

local Gloves_Cfg=ServerOHOH.config.Gloves

--关卡解锁触发
-- stage/level_{id}/rebirth: value
-- stage/level_{id}/glove: value
-- stage/level_{id}/petBoost: value
local LV_UNLOCK={
    Progression="stage/level_%ARG1/%ARG2",
    keyList={"rebirth","glove","petBoost"}
}

-- 重生触发
-- rebirth/rebirth_{id}/level: value
-- rebirth/rebirth_{id}/glove: value
-- rebirth/rebirth_{id}/petBoost: value
local REBIRTH_ADD={
    Progression="rebirth/rebirth_%ARG1/%ARG2",
    keyList={"level","glove","petBoost"}
}

--买武器触发
-- glove/glove_{id}/level: value
-- glove/glove_{id}/rebirth: value
-- glove/glove_{id}/petBoost: value
local GLOVE_BUY={
    Progression="glove/glove_%ARG1/%ARG2",
    keyList={"level","rebirth","petBoost"}
}

--买拖尾触发
-- trail/trail_{id}/level: value
-- trail/trail_{id}/rebirth: value
-- trail/trail_{id}/petBoost: value
-- trail/trail_{id}/glove: value
local TRAIL_BUY={
    Progression="trail/trail_%ARG1/%ARG2",
    keyList={"level","rebirth","petBoost","glove"}
}

--摇蛋触发
-- hatch/egg_{id}/level: value
-- hatch/egg_{id}/rebirth: value
-- hatch/egg_{id}/petBoost: value
-- hatch/egg_{id}/glove: value
local HATCH_BUY={
    Progression="hatch/egg_%ARG1/%ARG2",
    keyList={"level","rebirth","petBoost","glove"}
}

--内购触发
-- purchase/item_{id}/level: value
-- purchase/item_{id}/rebirth: value
-- purchase/item_{id}/petBoost: value
-- purchase/item_{id}/glove: value
local PURCHASE_BUY={
    Progression="purchase/item_%ARG1/%ARG2",
    keyList={"level","rebirth","petBoost","glove"}
}

--好友邀请触发
-- invite/level_{id}/invite_num: value
local INVITE_BUY={
    Progression="invite/num_%ARG1/%ARG2",
    keyList={"level"}
}

local function CreateGAKey(str,value)
    for k,v in pairs(value) do
        str=string.gsub(str,"%%ARG"..k,v,1) 
    end
	return str
end
function M.new(self,...)
	return M(self,...);
end;

--获取当前重生次数
function M.rebirth(self)
    return self.DataBase:GetData("base").rebirth
end
--获取当前关卡
function M.level(self)
    return self.DataBase:GetData("base").level
end
--获取当前手套加成
function M.glove(self)
    return Gloves_Cfg[self.DataBase:GetData("glove").equip].boost
end
--获取宠物加成和
function M.petBoost(self)
   return self.PetCtrl:GetPetBoost()
end
--获取邀请好友数量
function M.invite_num(self)
end

function M.ctor(self, parent)
	M.super.ctor(self, parent);

    self.DataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())
    -- self.PetCtrl=self.parent:GetComponent(_G.CommonOHOH.load.Common_PetCtrl())

    self.OldLevel= self.DataBase:GetData("base").level
    self.OldRebirth= self.DataBase:GetData("base").rebirth
    

    self.parent.UserDataEvent:Connect(function(key)
        if key=="base" then
            local baseData=self.DataBase:GetData("base")
            if self.OldLevel~=baseData.level then
                -- print("GA关卡-->",baseData.level)
                self:SendProgression(LV_UNLOCK,baseData.level)
                self.OldLevel=baseData.level
            end
            if self.OldRebirth~=baseData.rebirth then
                -- print("GA重生-->",baseData.level)
                self:SendProgression(REBIRTH_ADD,baseData.rebirth)
                self.OldRebirth=baseData.rebirth
            end
        end
    end)

    -- --购买拖尾监听
    -- self.parent:GetComponent(ServerOHOH.load.Server_TrailingCtrl()).BuyEvent:Connect(function(id)
    --     -- print("GA拖尾-->",id)
    --     self:SendProgression(TRAIL_BUY,id)
    -- end)

    -- --购买拳套监听
    -- self.parent:GetComponent(ServerOHOH.load.Server_GloveCtrl()).BuyEvent:Connect(function(id)
    --     -- print("GA拳套-->",id)
    --     self:SendProgression(GLOVE_BUY,id)
    -- end)

    --摇蛋监听
    self.parent.HatchEggEvent:Connect(function(id)
        -- print("GA摇蛋-->",id)
        self:SendProgression(HATCH_BUY,id)
    end)


    --发生内购事件
    self.parent.PurchaseEvent:Connect(function(id)
        -- print("GA内购Id-->",id)
        self:SendProgression(PURCHASE_BUY,id)
    end)

    -- --邀请好友事件
    -- self.parent:GetComponent(ServerOHOH.load.Server_FriendBoostCtrl()).boost.Changed:Connect(function(value)
    --     -- print("GA好友加成-->",value)
    --     self:SendProgression(INVITE_BUY,value)
    -- end)
    
    
end;

function M.dtor(self)
	M.super.dtor(self);
end;

function M.SendProgression(self,event,id)
    self:AddProcess(function()
        local sendList ={}
        for _,fucName in pairs(event.keyList) do
            local v=0
            local keyArg={}
            if M[fucName] then
                v=M[fucName](self)
            end
            table.insert(keyArg,tostring(id))
            table.insert(keyArg,fucName)
            table.insert(sendList,{
                key=CreateGAKey(event.Progression,keyArg),
                score=v
            })
        end
        self:AddProgressionStageEvent(sendList)
    end)
end

--传入数据结构eg:
-- data={
--    [1]={key=hatch/egg_id/glove，score=50}
-- }
function M.AddProgressionStageEvent(self,list)
    for _,data in pairs(list) do
        local keylist=string.split(data.key,"/")
        -- local str=""
        -- for _,v in pairs(keylist) do
        --     str=str.."/"..v
        -- end
        -- str=str..":"..data.score
        -- print("GA-->",str)
        _G.GameAnalytics:addProgressionEvent(self.parent:GetUserId(), {
            progressionStatus = _G.GameAnalytics.EGAProgressionStatus.Complete,
            progression01 =keylist[1],
            progression02 =keylist[2],
            progression03 =keylist[3],
            score = data.score
        })
    end
end


return M